package com.rlyy.basic.process;

import com.rlyy.common.enums.CommonEnum;
import com.rlyy.common.exception.BusinessException;
import com.rlyy.basic.mapping.RequestHandlerMapping;
import com.rlyy.common.param.Request;
import com.rlyy.common.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;

import java.util.List;

/**
 * 执行调度器
 *
 * @author yuanqinglong
 * @since 2021/9/14 12:08
 */
@Component
public class DefaultProcessorDispatcher extends AbstractProcessorDispatcher {

    @Autowired
    private RequestHandlerMapping requestHandlerMapping;

    public DefaultProcessorDispatcher(List<ISyncAsyncProcessor> syncAsyncProcessors) {
        super(syncAsyncProcessors);
    }

    /**
     * 处理请求
     *
     * @param request 完整的请求
     * @return 结果
     */
    @Override
    public Result process(Request request) throws Exception{
        HandlerMethod handlerMapping = requestHandlerMapping.getHandlerMapping(request.getApiId());
        if (handlerMapping == null) {
            throw new BusinessException(CommonEnum.API_NOT_FIND);
        }
        ISyncAsyncProcessor syncAsyncProcessor = super.getSyncAsyncProcessor();
        return syncAsyncProcessor.doProcess(request);
    }


}
